<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/table.php";

class Gruppen extends table {
    
    public static $TABLENAME = "gruppen";
    public static $CLASSNAME = "Gruppen";
    
    public $id = "";
    public $name = "";
    public $plaetze = 0;
    public $foerderung = "Keine";
    
    public function __construct($id="") {
		if($id!='') {
            $temp = Database::getDefault()->openId(Gruppen::$TABLENAME,$id);
            if($temp!=null) {
                foreach($temp as $key => $value) {
                    $this->$key = $value;
                }
            }
		}
    }
    
    public function delete() {
        Database::getDefault()->delete(Gruppen::$TABLENAME,$this->id);
    }
    
    public function save() {
    	$values = array();
		foreach ($this as $key => $value) {
			$values[$key] = $value;
		}
        if($this->id=="") {
            $this->id = Database::getDefault()->insert(Gruppen::$TABLENAME, $values);
        } else {
            Database::getDefault()->update(Gruppen::$TABLENAME, $values, $this->id);
        }
    }
	
    /**
     * Get all groups
     */
    static public function getAll() {
        global $VEREIN;
        $query = "SELECT * FROM " . $VEREIN['prefix'] . Gruppen::$TABLENAME . " ORDER BY name";
        return Database::getDefault()->execute($query,Gruppen::$CLASSNAME);
    }

    public function getId() {
        return $this->id;
    }
    
    public function setName($value) {
        $this->name = $value;
    }
    
    public function getName() {
        return $this->name;
    }
    
    public function setPlaetze($value) {
        $this->plaetze = $value;
    }
    
    public function getPlaetze() {
        return $this->plaetze;
    }
    
    public function setFoerderung($value) {
        $this->foerderung = $value;
    }
    
    public function getFoerderung() {
        return $this->foerderung;
    }
    
	public function getFoerderungValues() {
	    global $VEREIN;
		$query = "SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='" . $VEREIN['prefix'] . Gruppen::$TABLENAME . "' AND COLUMN_NAME='foerderung';";
		return Database::getDefault()->execute($query);
	}
    
    /**
     * create table for addresses or update if something to do
     */
    public static function initDatabase() {
        global $VEREIN;
        echo "<b>create / update ". $VEREIN['prefix'] . Gruppen::$TABLENAME . "</b><br />";
        $query = "CREATE TABLE IF NOT EXISTS " . $VEREIN['prefix'] . Gruppen::$TABLENAME . " (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            die(Database::getDefault()->getConnection()->error);
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Gruppen::$TABLENAME . " ADD COLUMN name VARCHAR(100) NOT NULL";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Gruppen::$TABLENAME . " ADD COLUMN plaetze INT UNSIGNED NOT NULL DEFAULT 0";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Gruppen::$TABLENAME . " ADD COLUMN foerderung VARCHAR(50) DEFAULT NULL";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        echo "<br />";
    }

}
?>